const { Sequelize, DataTypes } = require('sequelize');
const sequelize = new Sequelize(process.env.DB_CONNECTION_STRING);

const Report = sequelize.define('Report', {
    title: {
        type: DataTypes.STRING,
        allowNull: false
    },
    author: {
        type: DataTypes.STRING,
        allowNull: false
    },
    content: {
        type: DataTypes.TEXT,
        allowNull: false
    },
    filePath: {
        type: DataTypes.STRING,
        allowNull: false
    },
    createdAt: {
        type: DataTypes.DATE,
        defaultValue: DataTypes.NOW
    },
    updatedAt: {
        type: DataTypes.DATE,
        defaultValue: DataTypes.NOW
    }
}, {
    tableName: 'reports'
});

const createReport = async (reportData) => {
    return await Report.create(reportData);
};

const getReportById = async (id) => {
    return await Report.findByPk(id);
};

const getAllReports = async () => {
    return await Report.findAll();
};

const updateReport = async (id, reportData) => {
    return await Report.update(reportData, {
        where: { id }
    });
};

const deleteReport = async (id) => {
    return await Report.destroy({
        where: { id }
    });
};

module.exports = {
    Report,
    createReport,
    getReportById,
    getAllReports,
    updateReport,
    deleteReport
};